package com.hotel.controller.htchangerooms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.controller.htcheckout.HtCheckOutController;
import com.hotel.dto.HtChangeRoomsDto;
import com.hotel.entity.*;
import com.hotel.service.htchangerooms.HtChangeRoomsService;
import com.hotel.service.htcheckin.HtCheckInService;
import com.hotel.service.htroom.HtRoomService;
import com.hotel.service.htroomtype.HtRoomTypeService;
import com.hotel.util.DateUtils;
import com.hotel.util.R;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/Variable")
@RestController
@CrossOrigin
public class HtChangeRoomsController {

    @Resource
    private HtRoomService htRoomService;

    @Resource
    private HtRoomTypeService htRoomTypeService;

    @Resource
    private HtChangeRoomsService htChangeRoomsService;

    @Resource
    private HtCheckInService htCheckInService;

    private DateUtils dateUtils = new DateUtils();

    @PostMapping("/list")
    public R htChangeRoomsList(HtChangeRoomsDto htChangeRoomsDto) {
        try {
            //构造分页构造器对象
            Page<HtChangeRooms> pageInfo = new Page<>(htChangeRoomsDto.getPage(), htChangeRoomsDto.getLimit());
            Page<HtChangeRoomsDto> htChangeRoomsPage = new Page<>();

            LambdaQueryWrapper<HtChangeRooms> ChangeRoomslqw = new LambdaQueryWrapper<>();
            ChangeRoomslqw.like(htChangeRoomsDto.getCheckInNo() != null && htChangeRoomsDto.getCheckInNo() != "", HtChangeRooms::getCheckInNo, htChangeRoomsDto.getCheckInNo());
            ChangeRoomslqw.like(htChangeRoomsDto.getCheckInPerson() != null && htChangeRoomsDto.getCheckInPerson() != "", HtChangeRooms::getCheckInPerson, htChangeRoomsDto.getCheckInPerson());
            ChangeRoomslqw.like(htChangeRoomsDto.getNewRoomNo() != null && htChangeRoomsDto.getNewRoomNo() != 0, HtChangeRooms::getNewRoomNo, htChangeRoomsDto.getNewRoomNo());
            if (htChangeRoomsDto.getRoomTypeNo().equals("0")) {
                htChangeRoomsDto.setRoomTypeNo(null);
            }
            ChangeRoomslqw.like(htChangeRoomsDto.getRoomTypeNo() != null && htChangeRoomsDto.getRoomTypeNo() != 0, HtChangeRooms::getRoomTypeNo, htChangeRoomsDto.getRoomTypeNo());

            // 时间范围查询
            if (htChangeRoomsDto.getArrivalTimeAll() != null && htChangeRoomsDto.getArrivalTimeAll() != "") {
                String str = htChangeRoomsDto.getArrivalTimeAll();
                String[] strs = str.split(",");
                ChangeRoomslqw.ge(HtChangeRooms::getArrivalTime, strs[0]);
                ChangeRoomslqw.le(HtChangeRooms::getArrivalTime, strs[1]);
            }
            if (htChangeRoomsDto.getDepartureTimeAll() != null && htChangeRoomsDto.getDepartureTimeAll() != "") {
                String str = htChangeRoomsDto.getDepartureTimeAll();
                String[] strs = str.split(",");
                ChangeRoomslqw.ge(HtChangeRooms::getDepartureTime, strs[0]);
                ChangeRoomslqw.le(HtChangeRooms::getDepartureTime, strs[1]);
            }


            htChangeRoomsService.page(pageInfo, ChangeRoomslqw);

            //对象拷贝
            BeanUtils.copyProperties(pageInfo, htChangeRoomsPage);

            List<HtChangeRooms> htChangeRooms = pageInfo.getRecords();

            List<HtChangeRoomsDto> list = htChangeRooms.stream().map((item) -> {

                HtChangeRoomsDto htChangeRoomsDto1 = new HtChangeRoomsDto();

                BeanUtils.copyProperties(item, htChangeRoomsDto1);

                Long roomTypeNo = item.getRoomTypeNo();
                //根据id查询分类对象
                HtRoomType htRoomType = htRoomTypeService.getById(roomTypeNo);

                if (htRoomType != null) {
                    String htRoomTypeName = htRoomType.getRoomTypeName();
                    htChangeRoomsDto1.setRoomTypeName(htRoomTypeName);
                }

                List<HtRoomType> htRooTypeList = htRoomTypeService.list();
                htRooTypeList.add(0, new HtRoomType((long) 0, "请选择"));
                htChangeRoomsDto1.setHtRoomTypeList(htRooTypeList);
                return htChangeRoomsDto1;
            }).collect(Collectors.toList());


            htChangeRoomsPage.setRecords(list);

            return new R(R.OK, htChangeRoomsPage);
        } catch (Exception e) {
            return new R(R.SERVER_ERROR);
        }
    }

    @PostMapping("/add")
    public R htChangeRoomsAdd(HtChangeRooms htChangeRooms) {

        LambdaQueryWrapper<HtRoom> htRoomLQW = new LambdaQueryWrapper<>();
        htRoomLQW.eq(htChangeRooms.getNewRoomNo() != null && htChangeRooms.getNewRoomNo() != 0, HtRoom::getRoomNo, htChangeRooms.getNewRoomNo());
        HtRoom htRoom = htRoomService.getOne(htRoomLQW);

        if (htRoom.getStatusNo().equals("1")) {
            return new R(R.SERVER_ERROR, "该房间已预定");
        } else if (htRoom.getStatusNo().equals("3")) {
            return new R(R.SERVER_ERROR, "该房间已入住");
        } else {
            LambdaQueryWrapper<HtCheckIn> htCheckInLQW = new LambdaQueryWrapper<>();
            htCheckInLQW.eq(htChangeRooms.getIdNumber() != null && htChangeRooms.getIdNumber() != "", HtCheckIn::getIdNumber, htChangeRooms.getIdNumber());
            htCheckInLQW.eq(htChangeRooms.getIdType() != null && htChangeRooms.getIdType() != "", HtCheckIn::getIdType, htChangeRooms.getIdType());
            HtCheckIn one = htCheckInService.getOne(htCheckInLQW);

            HtChangeRooms htChangeRooms1 = new HtChangeRooms();
            htChangeRooms1.setCheckInNo(one.getCheckInNo());
            htChangeRooms1.setArrivalTime(one.getArrivalTime());
            htChangeRooms1.setDepartureTime(one.getDepartureTime());
            htChangeRooms1.setCheckInPerson(one.getCheckInPerson());
            htChangeRooms1.setIdNumber(one.getIdNumber());
            htChangeRooms1.setIdType(one.getIdType());
            htChangeRooms1.setOldRoomNo(one.getRoomNo());
            htChangeRooms1.setRoomTypeNo(htChangeRooms.getRoomTypeNo());
            htChangeRooms1.setCashPledge(one.getCashPledge());
            htChangeRooms1.setStandardPrice(htChangeRooms.getStandardPrice());
            htChangeRooms1.setNewRoomNo(htChangeRooms.getNewRoomNo());
            htChangeRooms1.setDiscountedPrice(one.getDiscountedPrice());
            htChangeRooms1.setPhone(one.getPhone());
            htChangeRooms1.setOccupancyNumber(htChangeRooms.getOccupancyNumber());
            htChangeRooms1.setMemberNo(one.getMemberNo());
            htChangeRooms1.setServeBreakfast(one.getServeBreakfast());
            htChangeRooms1.setTimingWakeUp(one.getTimingWakeUp());
            htChangeRooms1.setChangeRoomsRemark(one.getCheckInRemark() + "," + htChangeRooms.getChangeRoomsRemark());

            htChangeRoomsService.save(htChangeRooms1);

            HtRoom byId = htRoomService.getById(one.getRoomNo());
            byId.setStatusNo("2");
            htRoomService.updateById(byId);
            HtRoom byIds = htRoomService.getById(htChangeRooms.getNewRoomNo());
            byIds.setStatusNo("3");
            htRoomService.updateById(byIds);

            one.setCheckInRemark("已换客房");
            htCheckInService.updateById(one);
        }
        return new R(R.OK);
    }

    @PostMapping("/save")
    public R htChangeRoomsSave(HtChangeRooms htChangeRooms){
        if (htChangeRooms.getOperator().equals("null")) {
            htChangeRooms.setOperator(null);
        }

        LambdaQueryWrapper<HtChangeRooms> lqw = new LambdaQueryWrapper<>();
        lqw.eq(HtChangeRooms::getCheckInNo,htChangeRooms.getCheckInNo());
        HtChangeRooms one = htChangeRoomsService.getOne(lqw);

        if (one.getNewRoomNo().equals(htChangeRooms.getNewRoomNo())){
            htChangeRoomsService.updateById(htChangeRooms);
        }else {
            LambdaQueryWrapper<HtRoom> lqwHtRoom = new LambdaQueryWrapper<>();
            lqwHtRoom.eq(HtRoom::getRoomNo,one.getNewRoomNo());
            HtRoom htRoom = htRoomService.getOne(lqwHtRoom);
            HtRoom htRooms = htRoomService.getById(one.getNewRoomNo());
            htRooms.setStatusNo("2");
            htRoomService.updateById(htRooms);
            if (htRoom != null && htRoom.getStatusNo().equals("3")) {
                return new R(R.SERVER_ERROR,"该房间已入住");
            }else if (htRoom != null && htRoom.getStatusNo().equals("1")){
                return new R(R.SERVER_ERROR,"该房间已预定");
            }else {
                htRoom.setStatusNo("3");
                htRoomService.updateById(htRoom);
                htChangeRoomsService.updateById(htChangeRooms);
            }
        }

        return new R(R.OK);
    }


    @PostMapping("/delete")
    public R htChangeRoomsDelete(HtChangeRooms htChangeRooms){
        LambdaQueryWrapper<HtRoom> lqw = new LambdaQueryWrapper<>();
        lqw.eq(HtRoom::getRoomNo,htChangeRooms.getNewRoomNo());
        HtRoom htRoom = htRoomService.getOne(lqw);
        htRoom.setStatusNo("2");
        htRoomService.updateById(htRoom);
        LambdaQueryWrapper<HtChangeRooms> htChangeRoomslqw = new LambdaQueryWrapper<>();
        htChangeRoomslqw.eq(HtChangeRooms::getCheckInNo,htChangeRooms.getCheckInNo());
        HtChangeRooms one = htChangeRoomsService.getOne(htChangeRoomslqw);
        one.setChangeRoomsRemark("已经离开");
        one.setDepartureTime(dateUtils.strToDateLong(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
        htChangeRoomsService.updateById(one);
        HtCheckOutController htCheckOutController = new HtCheckOutController();
        HtCheckOut htCheckOut = new HtCheckOut();
        htCheckOut.setCheckInNo(htChangeRooms.getCheckInNo());
        htCheckOutController.htCheckOutSave(htCheckOut);
        return new R(R.OK);
    }
}
